<?php

namespace Home\Controller;

class DepartmentController extends HomeCoreController {
	
	public $Model = null;

    protected function _initialize(){
		
        parent::_initialize();
		
        $this->Model = D('Department');
		
    }
	
	public function index(){
		
		if(IS_AJAX){
						
			$mark = I('get.mark');
			
			$this->assign('mark', $mark);
			
			$search = I('get.search');
			
			if (!empty($search)){
				
                $map[$mark] = array('like', "%$search%");
				
			}
			
			$map['uid'] = session(C('AUTH_KEYS'));
			
			$page_size = 10;
			
			$count = $this->Model->where($map)->count();
			
			$Page = new \Think\Page($count, $page_size);
			
			$list = $this->Model
				->where($map)
				->order("id desc")
				->limit($Page->firstRow . ',' . $Page->listRows)
				->select();
			
			$str = $this->tojson($list, $count);
			
			echo $str;
			
			exit;
			
        }else{
			
            $this->display();
			
        }
		
    }
	
	public function dname(){
		
		$name = I('post.name');
		
		$department = I('post.param');
		
		$m = M('Department');
		
		$map['uid'] = session('gid');
		
		$map['title'] = $department;
		
		$data = $m->where($map)->find();
		
		if($data) {
			
			echo "y";
		 
		}else {
			
			echo "此部门名称已存在";
			
		}
		
	}
	
	public function add(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$data = $this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->update($data);
				
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
				
			}else{
				
				$error = $this->Model->getError();
				
				echo json_encode(array('info'=>$error, 'status'=>'n'));
				
			}	
					
		}else{
			
			$this->display();
			
		}
		
	}
	
	public function edit(){
		
		if(IS_POST){
			
			$post_data = I('post.');
			
			$data=$this->Model->create($post_data);
			
			if($data){
				
				$result = $this->Model->where(array('id'=>$post_data['id']))->update($data);
				
				if($result){
					
					echo json_encode(array('info'=>'操作成功', 'status'=>'y'));
					
				}else{
					
					$error = $this->Model->getError();
					
					echo json_encode(array('info'=>$error, 'status'=>'n'));
					
				}
				
			}else{
				
                $error = $this->Model->getError();
				
                echo json_encode(array('info'=>$error, 'status'=>'n'));
				
			}
			
		}else{
			
			$info=I('get.');
			
			$info = $this->Model->where(array('id'=>$info['id']))->find();
			
			$this->assign('info', $info);
			
        	$this->display();
			
		}
		
	}
	
	public function del(){
		
		$ids = I('get.ids');
		
        $map['id'] = array('in', $ids);
		
        $res = $this->Model->where($map)->delete();
		
        if($res){
			
            echo json_encode(array('info'=>'删除成功', 'status'=>'y'));
			
        }else{
			
            $error = $this->Model->getError();
			
			echo json_encode(array('info'=>$error, 'status'=>'n'));
			
        }
		
	}
	
}